#include "taskdetaildialog.h"
#include "ui_taskdetaildialog.h"
#include "global_var.h"
#include "utils.h"

#include <QMap>
#include <QFileDialog>
#include <QDebug>
#include <QDir>
#include <QMessageBox>

TaskDetailDialog::TaskDetailDialog(Task& task, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::TaskDetailDialog)
{
    ui->setupUi(this);
    ui->tbv_hand_in->setEditTriggers(QAbstractItemView::NoEditTriggers);

    this->task = task;

    this->hand_in_model = new QStandardItemModel();
    QStringList headers = tr("成员名,文件名").simplified().split(",");
    this->hand_in_model->setHorizontalHeaderLabels(headers);

    if(current_user.getId() != this->task.getTask_owner()) {
        ui->btn_download->setEnabled(false);
    }
    if(this->task.getMember_hand_in_state_map().keys().indexOf(current_user.getId()) == -1) {
        ui->btn_upload->setEnabled(false);
    }

    init();
}

TaskDetailDialog::~TaskDetailDialog()
{
    delete ui;
}

void TaskDetailDialog::init()
{
    QStandardItem* item = NULL;
    QMap<QString, QString> status_map = task.getMember_hand_in_state_map();

    ui->lbl_tname->setText(task.getTask_name());
    ui->lbl_manager->setText(task.getTask_owner());
    ui->end_time->setMinimumDateTime(QDateTime::currentDateTime());
    ui->end_time->setDateTime(task.getEnd_time());

    QStringList key_list = status_map.keys();
    for(int i=0; i < key_list.size(); i++) {
        item = new QStandardItem(key_list[i]);
        this->hand_in_model->setItem(i, 0, item);

        item = new QStandardItem(status_map[key_list[i]]);
        this->hand_in_model->setItem(i, 1, item);
    }

    update_table_view();
}

void TaskDetailDialog::update_table_view()
{
    ui->tbv_hand_in->setModel(this->hand_in_model);
    ui->tbv_hand_in->show();
}

void TaskDetailDialog::on_btn_upload_clicked()
{
    QFileDialog* file_dialog = new QFileDialog(this);
    file_dialog->setWindowTitle(tr("打开文件"));
    file_dialog->setDirectory(QDir::rootPath());
    file_dialog->setFileMode(QFileDialog::ExistingFile);
    file_dialog->setViewMode(QFileDialog::Detail);

    QString file_name;
    if(file_dialog->exec()) {
        file_name = file_dialog->selectedFiles()[0];
    }

    QString file_path = this->task.getSave_path() + QDir::separator() + file_name.split(QDir::separator()).last();

    bool success = QFile::copy(file_name, out_path + QDir::separator() + file_path);
    if(!success) {
        QMessageBox::information(this, tr("Error"), tr("上传失败"), QMessageBox::Yes);
        return;
    }

    this->task.getMember_hand_in_state_map()[current_user.getId()] = file_name.split(QDir::separator()).last();
    get_task_by_name(this->task.getTask_name(), current_tasks).getMember_hand_in_state_map()[current_user.getId()] = file_name.split(QDir::separator()).last();
    get_task_by_name(this->task.getTask_name(), tasks).getMember_hand_in_state_map()[current_user.getId()] = file_name.split(QDir::separator()).last();
    QMessageBox::information(this, tr("Success"), tr("上传成功"), QMessageBox::Yes);
}

void TaskDetailDialog::on_btn_ok_clicked()
{
    this->close();
}
